home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / apps / 82 / bicalcv2.c < prev    next >
C/C++ Source or Header  |  1986-11-20  |  21KB  |  597 lines

  1.                                                 /* Alain Birtz, 16/2/86 */
  2. #include "gemdefs.h"
  3.  
  4. /************************************************************************/
  5.  
  6. #define WI_KIND         (MOVER|CLOSER|NAME)     /* can be moved, closed */
  7.                                                 /* and title exist      */
  8. #define NO_WINDOW       (-1)
  9.  
  10. #define Getrez()        (int)xbios(4)           /* return resolution    */
  11.  
  12. /************************************************************************/
  13.  
  14. extern int      gl_apid;
  15. extern long     xbios();
  16.  
  17. /************************************************************************/
  18.  
  19. int rez;                                        /* resolution number    */
  20. int ux,uy;                                      /* unit in pixels       */
  21. int box[28][4];                                 /* key                  */
  22. int blank;                                      /* screen state         */
  23. int dot;                                        /* dot in float. point  */
  24. int oper;                                       /* current operation    */
  25. int kb_conv[3][12]={
  26.         {0x31,0x32,0x33,0x7f,0x34,0x35,0x36,0x2b,0x37,0x38,0x39,0x2d},
  27.         {0x30,0x21,0xff,0x2a,0x2e,0x62,0x6f,0x2f,0x25,0x26,0x7c,0x3d},
  28.         {0x61,0x62,0x63,0x2a,0x64,0x65,0x66,0x2f,0x30,0x26,0x7c,0x3d}
  29. };
  30.  
  31. int     menu_id ;                               /* our menu id          */
  32. int     phys_handle;                            /* physical workstation */
  33. int     handle;                                 /* virtual workstation  */
  34. int     wi_handle;                              /* window handle        */
  35. int     top_window;                             /* handle of topped     */
  36.  
  37. int     x_init,y_init,w_init,h_init;
  38. int     xwork,ywork,hwork,wwork;                /* initial, work areas  */
  39.  
  40. int     msgbuff[8];                             /* event message buffer */
  41. int     mx,my;                                  /* mouse x and y pos.   */
  42. int     kb_code;                                /* keyboard return      */
  43. int     butdown;                                /* button state         */
  44. int     d;                                      /* dummy variable       */
  45.  
  46. int     contrl[12];                             /* AES, VDI variable    */
  47. int     intin[128];
  48. int     ptsin[128];
  49. int     intout[128];
  50. int     ptsout[128];
  51.  
  52. int work_in[11];                                /* Input GSX parameter  */
  53. int work_out[57];                               /* Output GSX parameter */
  54.  
  55. long base;                                      /* 10=DECI, 16=HEXA     */
  56. long temp_base;                                 /* used in octal,binary */
  57. long mem_val, scr_val;                          /* arithmetic value     */
  58. long mem_div=1L, scr_div=1L;                    /* used in float. point */
  59.  
  60. char key_symb[3][12]={
  61.         {'1','2','3','\275','4','5','6','+','7','8','9','-'},
  62.         {'0','!','\361','*','.','b','o','/','%','&','|','='},
  63.         {'A','B','C','*','D','E','F','/','0','&','|','='}
  64. };
  65. char hex_deci[][5]={"HEXA","DECI"};
  66. char chr[]=" ";                                 /* to print one char    */
  67.  
  68.  
  69. /************************************************************************/
  70.  
  71. set_unit()                                      /* according to rez.    */
  72. {
  73.         rez=Getrez();
  74.         ux=10;
  75.         uy=(rez==2 ? 10:5);
  76. }
  77.  
  78. /************************************************************************/
  79.  
  80. author()
  81. {
  82. long time;
  83.  
  84.         v_gtext(handle,box[26][0]+ux/2,box[26][3]-uy/2," by A.Birtz");
  85.         time=150000L;
  86.         while(time--)
  87.                 ;                               /* timeout              */
  88.         reset_v();
  89.         base=10L;                               /* initialisation       */
  90.         temp_base=10L;
  91.         oper=3;
  92.         display();                              /* clear author!        */
  93. }
  94.  
  95. /************************************************************************/
  96.  
  97. one_key(no,style)                               /* draw one key         */
  98. int no, style;
  99. {
  100. char *s;
  101.  
  102.         if (no<24)
  103.                 {
  104.                 chr[0]=key_symb[(no<12 ? 0:1+(base==16L))][no%12];
  105.                 s=chr;
  106.                 }
  107.         else
  108.                 s=hex_deci[no-24];
  109.  
  110.         graf_mouse(256,0);                      /* hide mouse           */
  111.         fill(box[no],style,0);                  /* clear and fill       */
  112.         vswr_mode(handle,(style) ? 3:1);        /* reverse if style=1   */
  113.                                                 /* print text           */
  114.         v_gtext(handle,box[no][0]+7*ux/10+2*(no>23),box[no][3]-4*uy/10,s);
  115.         vswr_mode(handle,1);                    /* normal printing      */
  116.         graf_mouse(257,0);                      /* show mouse           */
  117. }
  118.  
  119. /************************************************************************/
  120.  
  121. open_vwork()                                    /* open workstation     */
  122. {
  123. int i;
  124.         for(i=0;i<10;work_in[i++]=1);      
  125.                 work_in[10]=2;
  126.         handle=phys_handle;
  127.         v_opnvwk(work_in,&handle,work_out);
  128. }
  129.  
  130. /************************************************************************/
  131.  
  132. open_window()                                   /* open window          */
  133. {
  134.         wi_handle=wind_create(WI_KIND,x_init,y_init,w_init,h_init);
  135.         wind_set(wi_handle, WF_NAME," Calculator ",0,0);
  136.         wind_open(wi_handle,x_init,y_init,w_init,h_init);
  137.         wind_get(wi_handle,WF_WORKXYWH,&xwork,&ywork,&wwork,&hwork);
  138. }
  139.  
  140. /************************************************************************/
  141.  
  142. main()
  143. {
  144.         appl_init();
  145.         phys_handle=graf_handle(&d,&d,&d,&d);
  146.         menu_id=menu_register(gl_apid,"  Calculator");
  147.  
  148.         set_unit();
  149.  
  150.         x_init=5*ux;
  151.         y_init=5*uy;
  152.         w_init=15*ux;
  153.         h_init=31*uy;
  154.  
  155.         wi_handle=NO_WINDOW;
  156.         butdown=1;
  157.  
  158.         multi();
  159. }
  160.  
  161. /************************************************************************/
  162.  
  163. multi()
  164. {
  165. int event, k;
  166.  
  167.         while (1)
  168.                 {
  169.                 event = evnt_multi(MU_MESAG | MU_BUTTON | MU_KEYBD,
  170.                                 1,1,butdown,0,0,0,0,0,0,0,0,0,0,
  171.                                 msgbuff,0,0,&mx,&my,&d,&d,&kb_code,&d);
  172.  
  173.                 wind_update(1);
  174.                 wind_get(wi_handle,WF_TOP,&top_window,&d,&d,&d);
  175.  
  176.                 if (event & MU_MESAG)
  177.  
  178. /*..................................................begin switch........*/
  179. switch (msgbuff[0])
  180. {
  181.         case WM_NEWTOP:case WM_TOPPED:
  182.         if (msgbuff[3] == wi_handle)
  183.                 {
  184.                 wind_set(wi_handle,WF_TOP,0,0,0,0);
  185.                 draw();
  186.                 }
  187.         break;
  188.  
  189.         case AC_CLOSE:
  190.         if ((msgbuff[3] == menu_id)&&(wi_handle != NO_WINDOW))
  191.                 {
  192.                 v_clsvwk(handle);
  193.                 wi_handle = NO_WINDOW;
  194.                 }
  195.         break;
  196.  
  197.         case WM_CLOSED:
  198.         if (msgbuff[3] == wi_handle)
  199.                 {
  200.                 wind_close(wi_handle);
  201.                 wind_delete(wi_handle);
  202.                 v_clsvwk(handle);
  203.                 wi_handle = NO_WINDOW;
  204.                 graf_mouse(0,0);                /* arrow form           */  
  205.                 }
  206.         break;
  207.  
  208.         case WM_MOVED:
  209.         if (msgbuff[3] == wi_handle)
  210.                 {
  211.                 wind_set(wi_handle,WF_CURRXYWH,msgbuff[4],
  212.                                 msgbuff[5],msgbuff[6],msgbuff[7]);
  213.                 wind_get(wi_handle,WF_WORKXYWH,&xwork,&ywork,&wwork,&hwork);
  214.                 draw();
  215.                 }
  216.         break;
  217.  
  218.         case AC_OPEN:
  219.         if (msgbuff[4] == menu_id && wi_handle == NO_WINDOW)
  220.                 {
  221.                 open_vwork();
  222.                 open_window();
  223.                 draw();
  224.                 author();
  225.                 }
  226.         break;
  227.  
  228. }
  229. /*....................................................end switch........*/
  230.  
  231.                 if ((event & MU_BUTTON)&&(wi_handle == top_window))
  232.                         if (butdown)
  233.                                 {
  234.                                 if ((k=key_numb())>-1)
  235.                                         math(k);
  236.                                 butdown=0;
  237.                                 }
  238.                         else
  239.                                 butdown=1;
  240.                 
  241.                 if (event & MU_KEYBD)
  242.                         {
  243.                         if ((k=kb_ret())>-1)
  244.                                 math(k);
  245.                         }
  246.  
  247.                 wind_update(0);
  248.  
  249.         }                                       /* end of while (1)     */
  250.  
  251. }
  252.        
  253. /***********************************************************************/
  254.  
  255. reset_v()
  256. {
  257.         blank=1;
  258.         dot=0;
  259.         scr_val=mem_val=0L;
  260.         scr_div=mem_div=1L;
  261. }
  262.  
  263. /***********************************************************************/
  264.  
  265. plus()
  266. {
  267. long max_div;
  268.  
  269.         if (scr_div>1L || mem_div>1L)
  270.                 {
  271.                 max_div=(scr_div>mem_div ? scr_div:mem_div);
  272.                 scr_val=max_div/mem_div*mem_val + max_div/scr_div*scr_val;
  273.                 scr_div=max_div;
  274.                 }
  275.         else
  276.                 scr_val += mem_val;
  277. }
  278.  
  279. /***********************************************************************/
  280.  
  281. minus()
  282. {
  283. long max_div;
  284.  
  285.         if (scr_div>1L || mem_div>1L)
  286.                 {
  287.                 max_div=(scr_div>mem_div ? scr_div:mem_div);
  288.                 scr_val=max_div/mem_div*mem_val - max_div/scr_div*scr_val;
  289.                 scr_div=max_div;
  290.                 }
  291.         else
  292.                 scr_val=mem_val-scr_val;
  293. }
  294.  
  295. /***********************************************************************/
  296.  
  297. product()
  298. {
  299.         scr_val *= mem_val;
  300.         scr_div *= mem_div;
  301.         while (scr_div>100L)
  302.                 {
  303.                 scr_val /= 10L;
  304.                 scr_div /= 10L;
  305.                 }
  306. }
  307.  
  308. /***********************************************************************/
  309.  
  310. quotient()
  311. {
  312. long work;
  313.  
  314.         if (!scr_div)                           /* division by zero     */
  315.                 return;
  316.  
  317.         if (scr_div>1L || mem_div>1L)
  318.                 {
  319.                 work=100L;                      /*       ????           */
  320.                 scr_val=work/mem_div*mem_val*scr_div/scr_val;
  321.                 scr_div=100L;
  322.                 }
  323.         else
  324.                 scr_val=mem_val/scr_val;
  325. }
  326.  
  327. /************************************************************************/
  328.  
  329. math(k)
  330. int k;
  331. {
  332. int i;
  333. long fact;
  334.  
  335.         if (k>-1 && k<24)                       /* hit key signal       */
  336.                 {
  337.                 one_key(k,1);                   /* dark key             */
  338.                 for(i=0;i<20000;i++)
  339.                         ;                       /* time out             */
  340.                 one_key(k,0);                   /* white key end signal */
  341.                 }
  342.  
  343.         blank=0;
  344.         
  345.         if (k!=-1 && k<21 && k%4!=3)            /* digit (DECI or HEXA) */
  346.             if (base==16L || (k<13 && (!dot || scr_div<100L)))
  347.                 {
  348.                 scr_val *= base;
  349.                 scr_val += (long) ((3*(k/4) + k%4 + 1)%(base==10L ? 10:16));
  350.                 if (dot)
  351.                         scr_div *= 10L;
  352.                 }
  353.  
  354.         switch(k)
  355.         {
  356.         case 3:reset_v();break;                 /* clear                */                      case 7:case 11:case 15:case 19:case 21:case 22:
  357.                                                 /* + - * / & |          */
  358.                 mem_div=scr_div;scr_div=1L;
  359.                 mem_val=scr_val;scr_val=0L;
  360.                 oper=k;blank=1;dot=0;break;
  361.         case 13:                                /* factorial            */
  362.                 if (base==10L && scr_val>-1L && scr_val<16L)
  363.                         {
  364.                         i=1; fact=1L;
  365.                         while (i< (int) scr_val)
  366.                                 fact *= (long) ++i;
  367.                         scr_val=fact;
  368.                         }
  369.                 break;
  370.         case 14:if (base==10L)                  /* sign change          */
  371.                         scr_val= -scr_val;
  372.                 break;
  373.         case 16:if (base==10L)                  /* set float point dot  */
  374.                         dot=1;
  375.                 break;
  376.         case 17:case 18:                        /* binary and octal     */
  377.                 if (!dot && base==10L)
  378.                         {
  379.                         temp_base=(k==17 ? 2L:8L);
  380.                         if (k==17)              /* first 8 bits only    */
  381.                                 scr_val &= 0xff;
  382.                         }
  383.                 break;
  384.         case 20:if (base==10L)                  /* per cent (%)         */
  385.                         {
  386.                         while (mem_div<100L)
  387.                                 {
  388.                                 mem_div *= 10L; /* convert in float.    */
  389.                                 mem_val *= 10L;
  390.                                 }
  391.                         product();scr_val /= 100L;
  392.                         }
  393.                 break;
  394.         case 23:switch(oper)                    /* equal (=)            */
  395.                 {
  396.                 case  7:plus();break;
  397.                 case 11:minus();break;
  398.                 case 15:product();break;
  399.                 case 19:quotient();break;
  400.                 case 21:if (!dot)               /* 'and' operator       */
  401.                                 scr_val &= mem_val;
  402.                         break;
  403.                 case 22:                        /* 'or' operator        */
  404.                         if (!dot)
  405.                                 scr_val |= mem_val;
  406.                         break;
  407.                 default:break;
  408.                 }
  409.                 break;
  410.         case 24:case 25:                        /* HEXA or DECI         */
  411.                 base=(k==24 ? 16L:10L);
  412.                 one_key(24,(k==24));one_key(25,(k==25));
  413.                 for(i=12;i<21;i++)
  414.                         if (i%4!=3)             /* draw new key         */
  415.                                 one_key(i,0);
  416.                 if (dot)
  417.                         reset_v();
  418.                 if (mem_div>1L)
  419.                         {
  420.                         mem_val=0L;
  421.                         mem_div=1L;
  422.                         }
  423.                 break;
  424.         default:
  425.                 break;
  426.         }
  427.  
  428.         display();
  429. }
  430.  
  431. /************************************************************************/
  432.  
  433. fill(rect,style,index)
  434. int rect[], style, index;
  435. {
  436.         vsf_interior(handle,style);     /* fill inside the rect.        */
  437.         vsf_style(handle,index);        /* with index and style param.  */
  438.         v_bar(handle,rect);
  439. }
  440.  
  441. /************************************************************************/
  442.  
  443. draw()                                  /* box[][0] is left upper x     */
  444. {                                       /* box[][1] is left upper y     */
  445. int i, j;                               /* box[][2] is right lower x    */
  446. char chr[2];                            /* box[][3] is right lower y    */
  447.  
  448.         for(j=0;j<6;j++)                        /* 6 key row            */
  449.                 for(i=0;i<4;i++)                /* 4 column             */
  450.                 {
  451.                 box[i+4*j][0]=xwork+(2+3*i)*ux;
  452.                 box[i+4*j][2]=box[i+4*j][0]+2*ux;
  453.                 box[i+4*j][1]=ywork+(10+3*j)*uy;
  454.                 box[i+4*j][3]=box[i+4*j][1]+2*uy;
  455.                 }
  456.         for(i=0;i<2;i++)                        /* HEXA and DECI box    */
  457.         {
  458.         box[i+24][0]=xwork+(2+6*i)*ux;
  459.         box[i+24][2]=box[i+24][0]+5*ux;
  460.         box[i+24][1]=ywork+7*uy;
  461.         box[i+4*j][3]=box[i+24][1]+2*uy;
  462.         }
  463.         
  464.         box[26][0]=xwork+2*ux;                  /* show box             */
  465.         box[26][2]=xwork+13*ux;
  466.         box[26][1]=ywork+2*uy;
  467.         box[26][3]=ywork+5*uy;
  468.  
  469.         box[27][0]=xwork;                       /* calculator box       */
  470.         box[27][1]=ywork;
  471.         box[27][2]=xwork+wwork;
  472.         box[27][3]=ywork+hwork;
  473.  
  474.         graf_mouse(256,0);                      /* hide mouse           */
  475.  
  476.         fill(box[27],2,(rez==2) ? 6:5);         /* draw frame, grey     */
  477.  
  478.         for(i=0;i<25;i++)                       /* draw key box         */ 
  479.                 one_key(i,0);                   /* with white fill      */      
  480.         one_key(25,1);                          /* DECI active          */
  481.  
  482.         fill(box[26],0,0);                      /* draw show box        */
  483.         for(i=0;i<4;i++)                        /* reduce size          */
  484.                 box[26][i] += 3*(1-2*(i>1));
  485.         fill(box[26],0,0);                      /* interior box         */
  486.  
  487.         graf_mouse(3,0);                        /* extented finger      */
  488.         graf_mouse(257,0);                      /* show mouse           */
  489. }
  490.  
  491. /************************************************************************/
  492.  
  493. key_numb()      /* return the clicked key number or -1  if outside      */
  494. {
  495. int i;
  496.  
  497.         for(i=0;i<26;i++)
  498.                 if (mx>box[i][0] && mx<box[i][2]
  499.                                 && my>box[i][1] && my<box[i][3])
  500.                         return(i);
  501.  
  502.         return(-1);
  503. }
  504.  
  505. /************************************************************************/
  506.  
  507. kb_ret()                /* convert keyboard code to the key calculator  */
  508. {                       /* code, or return -1 if no valid key           */
  509. int i, b;
  510.  
  511.         kb_code &= 0xff;
  512.  
  513.         for(i=0;i<12;i++)
  514.                 if (kb_conv[0][i]==kb_code)
  515.                         return(i);
  516.  
  517.         b=((base==10L) ? 1:2);
  518.  
  519.         for(i=0;i<12;i++)
  520.                 if (kb_conv[b][i]==kb_code)
  521.                         return(i+12);
  522.  
  523.         return(-1);
  524. }
  525.  
  526. /************************************************************************/
  527.                                                                                 
  528. display()
  529. {
  530. int neg, s_index, l_div, i;
  531. long same, value, work_base, r;
  532. char s[12];
  533.  
  534.         for(i=0;i<12;i++)                       /* white space          */
  535.                 s[i]=' ';
  536.         s[i]='\0';
  537.  
  538.         if (blank)                              /* nothing to print     */
  539.                 {
  540.                 v_gtext(handle,box[26][0]+ux/2,box[26][3]-uy/2,s);
  541.                 return;
  542.                 }
  543.  
  544.         neg=0;
  545.         s_index=11;
  546.         same=value=scr_val;
  547.         l_div= (scr_div>1L)+(scr_div>10L);
  548.  
  549.         work_base=(!temp_base ? base:temp_base);
  550.  
  551.         if (value==0L)                          /* if zero              */
  552.                 {
  553.                 if (l_div)
  554.                         {
  555.                         for(i=0;i<l_div;i++)
  556.                                 s[s_index--]='0';
  557.                         s[s_index--]='.';
  558.                         }
  559.                 else
  560.                         if (dot)
  561.                                 s[s_index--]='.';
  562.                         else
  563.                                 s[s_index--]='0';
  564.                 }
  565.  
  566.         if (value<0L)                           /* if negative          */
  567.                 {
  568.                 same = value = -value;          /* make positive        */
  569.                 neg=1;                          /* set neg flag         */
  570.                 }
  571.  
  572.         while (value>0L)
  573.                 {
  574.                 if ((dot || scr_div>1L) && s_index==11-l_div)
  575.                         s[s_index--]='.';
  576.                 r = value;                      /* % don't work         */
  577.                 value /= work_base;             /* with long integer    */
  578.                 r -= work_base*value;           /* on DRI C             */
  579.                 s[s_index--]= (base==16L && r>9L ? 'A'-10:'0') + (int) r;
  580.                 }
  581.  
  582.         if (same && scr_div>same)
  583.                 {                               /* header . and 0       */
  584.                 i=l_div+s_index-11;
  585.                 while(i--)
  586.                         s[s_index--]='0';
  587.                 s[s_index--]='.';
  588.                 }
  589.  
  590.         temp_base=0L;
  591.  
  592.         if (neg==1)                             /* negative need sign - */
  593.                 s[s_index]='-';
  594.  
  595.         v_gtext(handle,box[26][0]+ux/2,box[26][3]-uy/2,s);
  596. }
  597.